package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 报表控制器
 */
@RestController
@RequestMapping("/report")
public class ReportController {


    @Reference
    private ReportService reportService;
    /**
     * 查询套餐预约占比和数量
     * @return
     */
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        try {
            Map rsMap = reportService.getSetmealReport();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,rsMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    /**
     * 查询用户报表数据
     *
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        try {
            Map rsMap = reportService.getMemberReport();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,rsMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }

    /**
     * 运营数据获取
     */
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            Map rsMap = reportService.getBusinessReportData();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,rsMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    /**
     * 运营数据导出到EXCEl
     */
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 获取数据
            Map rsMap = reportService.getBusinessReportData();
            String temlateRealPath = request.getSession().getServletContext().getRealPath("template") +
                    File.separator + "report_template.xlsx";
//            // 获取真实路径
//            String realPath = request.getSession().getServletContext().getContextPath();
//            // 获取模板文件所在路径
//            String templeFilePath = realPath + "template\\report_template.xlsx";
            // 加载模板文件
            File file = new File(temlateRealPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
            // 选择第一张表
            XSSFSheet sheet1 = workbook.getSheetAt(0);
            // 根据对应位置赋值
            sheet1.getRow(2).getCell(5).setCellValue((String)rsMap.get("reportDate"));
            sheet1.getRow(4).getCell(5).setCellValue((Integer)rsMap.get("todayNewMember"));
            sheet1.getRow(4).getCell(7).setCellValue((Integer)rsMap.get("totalMember"));
            sheet1.getRow(5).getCell(5).setCellValue((Integer)rsMap.get("thisWeekNewMember"));
            sheet1.getRow(5).getCell(7).setCellValue((Integer)rsMap.get("thisMonthNewMember"));
            sheet1.getRow(7).getCell(5).setCellValue((Integer)rsMap.get("todayOrderNumber"));
            sheet1.getRow(7).getCell(7).setCellValue((Integer)rsMap.get("todayVisitsNumber"));
            sheet1.getRow(8).getCell(5).setCellValue((Integer)rsMap.get("thisWeekOrderNumber"));
            sheet1.getRow(8).getCell(7).setCellValue((Integer)rsMap.get("thisWeekVisitsNumber"));
            sheet1.getRow(9).getCell(5).setCellValue((Integer)rsMap.get("thisMonthOrderNumber"));
            sheet1.getRow(9).getCell(7).setCellValue((Integer)rsMap.get("thisMonthVisitsNumber"));
            int row = 12;
            List<Map> setmealList = (List<Map>)rsMap.get("hotSetmeal");
            if (setmealList != null && setmealList.size() > 0) {
                for (Map map : setmealList) {
                    sheet1.getRow(row).getCell(4).setCellValue((String)map.get("name"));
                    sheet1.getRow(row).getCell(5).setCellValue((Long)map.get("setmeal_count"));
                    sheet1.getRow(row).getCell(6).setCellValue(((BigDecimal)map.get("proportion")).doubleValue());
                    sheet1.getRow(row).getCell(7).setCellValue((String)map.get("remark"));
                    row++;
                }
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            // 数据写出

            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            // 释放资源
            outputStream.flush();
            outputStream.close();
            workbook.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    {
         //  (String)rsMap.get("reportDate")
         //  (String)rsMap.get("totalMember")
         //  (String)rsMap.get("todayNewMember")
         //  (String)rsMap.get("thisWeekNewMember")
         //  (String)rsMap.get("thisMonthNewMember")
         //  (String)rsMap.get("todayOrderNumber")
         //  (String)rsMap.get("thisWeekOrderNumber")
         //  (String)rsMap.get("thisMonthOrderNumber")
         //  (String)rsMap.get("todayVisitsNumber")
         //  (String)rsMap.get("thisWeekVisitsNumber")
         //  (String)rsMap.get("thisMonthVisitsNumber")
         //  (String)rsMap.get("hotSetmeal")
    }

}
